<?php

return function ($oRes, $class_token, $db_fadm, $username, $password, $IPADDR) {
    $aWhere = array();
    $aWhere['user_name'] = $username;
    $sField = 'user_id,password';
    $mRow = $db_fadm->sql()->table('system_user')->where($aWhere)->field($sField)->find();
    if (empty($mRow)) {
        $this->error('用户名不存在');
    }
    if ($mRow['password'] != $password) {
        $this->error('密码错误');
    }
    unset($mRow['password']);
    $oRes->data('user_id', $mRow['user_id']);
    $where = array();
    $where['user_id'] = $mRow['user_id'];
    $mSave = array();
    $mSave['token_json'] = call_user_func(function($user_id) {
        //通过user_id就生成了token_json
        $token_info = array();
        $token_info['logined'] = microtime(TRUE);
        $token_info['actived'] = microtime(TRUE);
        $token_info['user_id'] = $user_id;
        return json_encode($token_info);
    }, $mRow['user_id']);
    $mSave['logined'] = 'CURRENT_TIMESTAMP()';
    $mSave['actived'] = 'CURRENT_TIMESTAMP()';
    $mSave['user_id'] = $mRow['user_id'];
    $mSave['login_ipaddr'] = $IPADDR;
    $mSave['error'] = NULL;
    $db_fadm->sql()->table('system_session')->where($where)->addwnesave($mSave);
    $db_fadm->commit();
    $oRes->setting('common_token', $class_token->TokenEncode($mSave['token_json']));
};
